package com.personal.system.exception;

import com.personal.common.result.Result;
import com.personal.common.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        log.error("全局异常....{}", e);
        e.printStackTrace();
        return Result.fail().message("全局异常处理:"+e.getMessage());
    }

    @ExceptionHandler(NullPointerException.class)
    public Result error(NullPointerException e) {
        log.error("空指针异常....{}", e);
        e.printStackTrace();
        return Result.fail().message("空指针异常处理:"+e.getMessage());
    }

    @ExceptionHandler(ArithmeticException.class)
    public Result error(ArithmeticException e) {
        log.error("特定异常.....{}", e);
        e.printStackTrace();
        return Result.fail().message("特定异常处理:"+e.getMessage());
    }

    @ExceptionHandler(GlobalException.class)
    public Result error(GlobalException e) {
        log.error("自定义异常.....{}",e);
        if (e.getMsg().contains("未登陆或token失效")) {
            return Result.fail().code(e.getCode()).message(e.getMsg());
        } else {
            return Result.fail().code(e.getCode()).message(e.getMsg());
        }
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        log.error("权限异常.....{}", e);
        e.printStackTrace();
        return Result.fail().code(ResultCodeEnum.PERMISSION.getCode()).message("没有当前功能操作权限");
    }
}
